import QtQuick 1.1
import com.nokia.symbian 1.1
import PictureItem 1.0
import "SettingsStorage.js" as Settings

PageStackWindow {
    id: appWindow

    initialPage: mainPage    

    property string str;
    property int storageToSet: 0
    property int defaultImageSize: PictureItem.QVGA
    property string defaultColor: "white"
    property int defaultMargin: 6
    property int defaultStorage: PictureItem.StorageInternal
    property int defaultAspectRatio: 1 // Qt::KeepAspectRatio

    PictureItem{
        id: picturePlugin

        onImageFinished:{
            if(success){
                errorDialog.title = qsTr("Image created")
                errorDialog.imageCreated = true
                errorDialog.text = "";                
                errorDialog.open();
            }else{
                errorDialog.title = qsTr("Error occured");
                errorDialog.text = qsTr("Failed to write image")
                errorDialog.open();
            }
        }

        onCanceled:{
            galleryPage.removeEmptyImages();
            pageStack.pop(mainPage);
            resultPage.busy = false;
        }

        onStorageChanged:{
            if(storage === PictureItem.StorageError){
                errorDialog.title = qsTr("Error occured");
                errorDialog.text = qsTr("Cannot change storage")
                errorDialog.open();
            }
        }
    }

    MainPage {
        id: mainPage
        onMaxImages: {
            galleryPage.maxNumOfImages = count
        }

        tools: ToolBarLayout {
            visible: true
            //anchors.bottom: parent.bottom
            ToolButton{
                flat: true
                iconSource: "toolbar-back";
                onClicked: Qt.quit();
            }

            ToolButton {
                flat: true
                iconSource: "toolbar-view-menu"
                anchors.right: (parent === undefined) ? undefined : parent.right
                onClicked: (mainPage.menu.status === DialogStatus.Closed) ? mainPage.menu.open() : mainPage.menu.close()
            }
        }

    }

    GalleryPage{
        id: galleryPage
        tools: ToolBarLayout {
            visible: true
            //anchors.bottom: parent.bottom
            ToolButton{
                iconSource: "toolbar-back";
                onClicked: {
                    galleryPage.clear();
                    pageStack.pop();
                }
            }

            ToolButton{
                iconSource: "toolbar-list"
                onClicked: {
                    galleryPage.createResult();
                }
            }
        }
    }

    TileResult{
        id: tileResult
        tools: ToolBarLayout {
            visible: true
            //anchors.bottom: parent.bottom
            ToolButton{
                iconSource: "toolbar-back";
                onClicked: {
                    galleryPage.removeEmptyImages();
                    pageStack.pop();
                }
            }

            ToolButton {
                iconSource: "toolbar-settings"
                anchors.right: (parent === undefined) ? undefined : parent.right
                onClicked: {
                    tileResult.setResultData();
                }
            }
        }

    }

    ListResult{
        id: listResult
        tools: ToolBarLayout {
            visible: true
            //anchors.bottom: parent.bottom
            ToolButton{
                iconSource: "toolbar-back";
                onClicked: {
                    listResult.setDefault();
                    pageStack.pop();
                }
            }

            ToolButton {
                iconSource: "toolbar-settings"
                anchors.right: (parent === undefined) ? undefined : parent.right
                onClicked: {
                    listResult.setResultData();
                }
            }
        }

    }

    ErrorDialog{
        id: errorDialog        

        onRejected: {
            if(!errorDialog.fromGallery){
                galleryPage.removeEmptyImages();
                pageStack.pop(mainPage);                
            }else{
                errorDialog.fromGallery = false;
            }
            resultPage.busy = false;
            errorDialog.imageCreated = false;
        }

        onAccepted: {
            if(!errorDialog.fromGallery){
                galleryPage.removeEmptyImages();
                pageStack.pop(mainPage);
                if(errorDialog.openImage){
                    errorDialog.openImage = false
                    console.log("picturePlugin.createdImageName: "+picturePlugin.createdImageName)
                    Qt.openUrlExternally(picturePlugin.createdImageName);
                }
            }else{
                errorDialog.fromGallery = false;
            }
            resultPage.busy = false;
            errorDialog.imageCreated = false;
        }
    }

    ResultQueryPage{        
        id: resultPage
        tools: ToolBarLayout {
            visible: visible
            //anchors.bottom: parent.bottom
            ToolButton{
                iconSource: "toolbar-back";
                onClicked: {
                    if(resultPage.busy)
                        picturePlugin.cancelDrawer();
                    pageStack.pop();
                }
            }
        }

    }

    Component.onCompleted: {        
        Settings.initialize();

        str = Settings.getSetting("imagesize");
        console.log("imgSize: "+str);
        if(str==="Unknown")
            str = Settings.setSetting("imagesize",defaultImageSize);
        console.log("imgSize: "+str);

        str = Settings.getSetting("background");
        console.log("bgColor: "+str);
        if(str==="Unknown")
            str = Settings.setSetting("background",defaultColor);
        console.log("bgColor: "+str);

        str = Settings.getSetting("margin");
        console.log("margin: "+str);
        if(str==="Unknown")
            str = Settings.setSetting("margin",defaultMargin);
        console.log("margin: "+str);

        str = Settings.getSetting("storage");
        console.log("storage: "+str);
        if(str==="Unknown")
            str = Settings.setSetting("storage",defaultStorage);
        console.log("storage: "+str);

        str = Settings.getSetting("aspectratio");
        console.log("aspectratio: "+str);
        if(str==="Unknown")
            str = Settings.setSetting("aspectratio",defaultAspectRatio);
        console.log("storage: "+str);

        str = Settings.getSetting("imagesize");
        mainPage.imageSize = str

        str = Settings.getSetting("background");
        mainPage.sheetColor = str

        str = Settings.getSetting("margin");
        mainPage.imageMargin = str
        picturePlugin.margin = mainPage.imageMargin

        str = Settings.getSetting("storage");
        mainPage.imageStorage = str
        picturePlugin.storage = mainPage.imageStorage

        str = Settings.getSetting("aspectratio");
        mainPage.imageAspectRatio = str
        picturePlugin.aspectRatio = mainPage.imageAspectRatio
    }
}
